package com.yan.test;

import com.yan.utils.DDD;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

/**
 * Created by yanl_ on 2018/3/21.
 */
public class TTT {


    private static Logger logger = LoggerFactory.getLogger(DDD.class);
    public static final String CODE_SUCCESS = "0000";
    public static final String CODE_PART_SUCCESS = "2000";
    public static final String[] ACCOUNT_VALIDATE_FILTER_WORDS = {"SDR", "IBU", "SDRI", "BPK"};

    /**
     * If the name is same then return "0000", if the name is similar than 80% then return "2000", else return ""
     *
     * @param inputName
     * @param returnName
     * @return
     */
    public static String checkName(String inputName, String returnName) {
        logger.info(String.format("inputName=%s   returnName=%s", inputName, returnName));

        String resultCode = "";

        //Filter the account name before compare
        String filteredReturnName = filterAccountName(returnName);
        String filterInputName = filterAccountName(inputName);

        logger.info("filterInputName={},  filteredReturnName={}", filterInputName, filteredReturnName);

        //名字相等
        if (filterInputName.equalsIgnoreCase(filteredReturnName)) {
            resultCode = CODE_SUCCESS;
            return resultCode;
        }

        int returnNameLength = returnName.length();
        boolean isContain = false;

        if (returnNameLength >= 20) {
            logger.info("returnNameLength is bigger than 19");
            int index = org.apache.commons.lang.StringUtils.indexOf(filterInputName, filteredReturnName);
            if (index == 0) {
                isContain = true;
            }
        }

        //名字前20位相等
        if (isContain) {
            resultCode = CODE_SUCCESS;
            return resultCode;
        }

        //检查名字配置度
        float similarRate = StringSimilarRateUtil.getSimilarRate(filterInputName, filteredReturnName);
        logger.info("similarRate={}, filteredReturnName={}, filterInputName={}", similarRate, filteredReturnName, filterInputName);
//        if (similarRate >= 0.8) {
//            logger.info("similarRate is bigger than 0.8 or equals to 0.8");
//            resultCode = CODE_PART_SUCCESS;
//            return resultCode;
//        }

        return resultCode;

    }

    //Filter the account name for filtering the SDR IBU SDRI BPK and space
    public static String filterAccountName(String name) {
        if (StringUtils.isEmpty(name)) {
            return "";
        }

        String[] splitedName = StringUtils.split(name, " ");

        if (splitedName != null && splitedName.length > 0) {
            String firstWord = splitedName[0].toUpperCase();
            if (Arrays.asList(ACCOUNT_VALIDATE_FILTER_WORDS).contains(firstWord)) {
                splitedName[0] = "";
            }
        }

        String formatName = StringUtils.join(splitedName, "");

        return formatName.toUpperCase();
    }


    public static float matchString(String var1, String var2) {
        int pos1 = 1;
        int pos2 = 1;
        int match_char1 = 0;
        int match_char2 = 0;
        if (var1 != null && !var1.equals("")) {
            var1 = var1.replace(" ", "").toLowerCase();
        }
        if (var2 != null && !var2.equals("")) {
            var2 = var2.replace(" ", "").toLowerCase();
        }
        if (var1 == null || var1.length() == 1) {
            return 0;
        } else if (var2 == null || var2.length() == 1) {
            return 0;
        }
         else if (var1.contains(var2) || var2.contains(var1)) {
            return 1;
        }

        else {
            for (int i = 1; i <= var1.length(); i++) {
                for (int j = pos1; j <= var2.length(); j++) {
                    String result1 = var1.substring(i - 1, i);
                    String result2 = var2.substring(j - 1, j);
                    System.out.println(result1 + " " + result2);
                    //if (var1.substring(i - 1, i).equals(var2.substring(j - 1, j))) {
                    if (result1.equals(result2)) {
                        match_char1 = match_char1 + 1;
                        pos1 = j + 1;
                        break;
                    }
                }
            }

            System.out.println("match_charlegth=" + match_char1 + "   length=" + var1.length());

            float out1 = (float) match_char1 / (float) var1.length();

            for (int i = 1; i <= var2.length(); i++) {
                for (int j = pos2; j <= var1.length(); j++) {
                    if (var2.substring(i - 1, i).equals(var1.substring(j - 1, j))) {
                        match_char2 = match_char2 + 1;
                        pos2 = j + 1;
                        break;
                    }
                }
            }
            System.out.println("match_charlegth=" + match_char2 + "   length=" + var2.length());
            float out2 = (float) match_char2 / (float) var2.length();
            return Math.max(out1, out2);
        }
    }


    public static void main(String[] args) {

        String sourceSubChannel = "0.8:0.5";
        String maxSimilarRate = sourceSubChannel.split(":")[0];
        String minSimilarRate = sourceSubChannel.split(":")[1];
        System.out.println(maxSimilarRate + "  " + minSimilarRate);

        String result = checkName("yanliang", "liangyan");
        logger.info("===="+result);

        float matchResult1 = matchString("Ganjar", "GANJAR INDRAWAN");
        logger.info("matchResult1:{}  ", Float.toString(matchResult1));
//        float matchResult2 = matchString("M reza fernandaGG", "MUHAMMAD REZA FERNANDA");
//        logger.info("matchResult2: {}", Float.toString(matchResult2));

//        float matchResult2 = matchString("yaa", "yanliang");
//        logger.info("matchResult2: {}", Float.toString(matchResult2));


        //    final long rawTimeout = TimeoutUtils.toMillis(timeout, unit);


        float matchResult3 = matchString("yanliang", "yangiang");
        logger.info("matchResult1:{}  ", Float.toString(matchResult3));

    }


}
